package com.moyun.finance.service;

import com.moyun.finance.entity.Customer;
import com.moyun.finance.entity.Product;
import com.moyun.finance.entity.Projects;
import com.moyun.finance.entity.Response;
import com.moyun.finance.repository.ProductRepository;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    ProductRepository pr;

    public Response save(Product product){
        product.setId(BaseUtil.uuid());
        pr.save(product);
        return new Response("添加成功!",true);
    }

    public Response update(Product product){
        pr.save(product);
        return new Response("产品信息修改成功!",true);
    }

    public Response delete(Product product){
        pr.delete(product);
        return new Response("产品删除成功",true);
    }

    //本公司所有产品
    public List<Product> findAll(String companyId){
        List<Product> products =  pr.findByCompanyId(companyId);
        return products;
    }

    public Product findOneById(String productId){
        return pr.findById(productId).orElse(null);
    }

    public List<Product> findByNameLike(String companyId, String productName){
        return pr.findByCompanyIdAndProductNameLike(companyId, "%"+productName+"%");
    }

    //根据子菜单查询
    public List<Product> findByAllType(String companyId, String allTypeId){
        List<Product> products =  pr.findByCompanyIdAndAllTypeId(companyId, allTypeId);
        return products;
    }
}
